﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SecsEquip;
using SecsEquip300;

namespace EquipSecs300
{
    public partial class CJobView : CMyFormEx
    {
        public GEM_CJob GEMCJob { get; set; }
        private int _CJobCountPre;

        public CJobView()
        {
            m_pViewName = "CJob";
            InitializeComponent();
            //初始化控件
            InitCtrl();
        }

        // 更新数据
        public override void UpdateData()
        {
            GEMCJob = secsEquipHelper.CJob;
            GEMCJob.CJobStageChange += OnListViewUpdata;
        }

        //初始化控件
        void InitCtrl()
        {
            listViewJob.Columns.Add("CJobID", 200, HorizontalAlignment.Left);
            listViewJob.Columns.Add("CreateTime", 150, HorizontalAlignment.Left);
            listViewJob.Columns.Add("UpdateTime", 150, HorizontalAlignment.Left);
            listViewJob.Columns.Add("State", 100, HorizontalAlignment.Left);
            listViewJob.Columns.Add("PJob List", 250, HorizontalAlignment.Left);

            OnPasswordLogin(GlobalVar.IsLogIn);
        }

        // 获取选中的CJobID
        string GetSelectPJobID()
        {
            string pCJobID = null;
            if (listViewJob.SelectedItems.Count > 0)
            {
                pCJobID = listViewJob.SelectedItems[0].SubItems[0].Text;
            }
            return pCJobID;
        }

        // 按钮
        private void BtnJobQueued_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.QUEUED);
        }

        private void btnJobSelect_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.SELECTED);
        }

        private void btnJobComplete_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.COMPLETED);
        }

        private void BtnJobStart_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.EXECUTING);
        }

        private void BtnJobPause_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.PAUSED);
        }

        private void BtnJobResume_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.EXECUTING);
        }

        private void BtnJobStop_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.COMPLETED);
        }

        private void BtnJobAbort_Click(object sender, EventArgs e)
        {
            GEMCJob.UpdateSelectJobState(GetSelectPJobID(), ControlJobState.COMPLETED);
        }

        // 删除
        private void btnJobDelete_Click(object sender, EventArgs e)
        {
            string pCJobID = GetSelectPJobID();
            GEMCJob.JobDelete(pCJobID);

        }

        private void BtnJobDeleteForce_Click(object sender, EventArgs e)
        {
            GEMCJob.JobDeleteAllForce();
        }

        private void btnRefesh_Click(object sender, EventArgs e)
        {
            ListViewUpdata();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (GEMCJob.CJobListAll.Count != _CJobCountPre)
            {
                _CJobCountPre = GEMCJob.CJobListAll.Count;
                ListViewUpdata();
            }
        }

        private void OnListViewUpdata()
        {
            BeginInvoke(new Action(() => { ListViewUpdata(); }));
        }
        private void ListViewUpdata()
        {
            listViewJob.Items.Clear();
            for (int j = 0; j < GEMCJob.CJobListAll.Count; j++)
            {
                ControlJob pCJob = GEMCJob.CJobListAll[j];
                // PJob列表
                List<string> listPJobID = new List<string>();
                for (int i = 0; i < pCJob.listProcessingCtrlSpec.Count; i++)
                {
                    listPJobID.Add(pCJob.listProcessingCtrlSpec[i].pPRJobID);
                }
                string pPJobList = CListOperate.listJoin(listPJobID);

                // 状态
                string pCJobState = pCJob.nCJobState.ToString();

                // 添加数据到窗口
                listViewJob.BeginUpdate();

                ListViewItem lvi = new ListViewItem();
                lvi.Text = pCJob.pCJobID;
                lvi.SubItems.Add(pCJob.pCJobTimeCreate);
                lvi.SubItems.Add(pCJob.pCJobTimeUpdate);
                lvi.SubItems.Add(pCJobState);
                lvi.SubItems.Add(pPJobList);

                listViewJob.Items.Add(lvi);

                listViewJob.EndUpdate();
            }

        }

        public override void OnPasswordLogin(int permission)
        {
            if (permission == 1)
            {
                panelPermission.Show();
            }
            else
            {
                panelPermission.Hide();
            }

        }


    }
}
